package com.expressage.controller;


import com.expressage.po.CityAddress;
import com.expressage.po.Recipient;
import com.expressage.service.AreaAddressService;
import com.expressage.service.CityAddressService;
import com.expressage.service.RecipientService;
import com.expressage.service.StreetAddressService;
import com.expressage.vo.AreaAddressVO;
import com.expressage.vo.RecipientVO;
import com.expressage.vo.StreetAddressVO;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * 收件人信息控制器
 */

@Controller
public class RecipientController {

    @Autowired
    RecipientService recipientService;
    @Autowired
    CityAddressService cityAddressService;
    @Autowired
    AreaAddressService areaAddressService;
    @Autowired
    StreetAddressService streetAddressService;


    Logger logger = Logger.getLogger(RecipientController.class);

    @RequestMapping("/recipient")
    public String findAll(Model model){
        logger.info("进入了收件人信息列表控制器");
        List<RecipientVO> list = recipientService.findAll();
        model.addAttribute("list",list);
        logger.info(list);
        return "jspafter/recipient";
    }

    @RequestMapping("/recipientlistone")
    public String findByReid(int reid,Model model){
        logger.info("进入了单个收件人信息控制器");
        RecipientVO recipient = recipientService.findByReid(reid);
        model.addAttribute("recipient",recipient);
        logger.info(recipient);
        return "jspafter/recipientlistone";
    }

    @RequestMapping("/toaddrecipient")
    public String toadd(Model model){
        List<RecipientVO> list =recipientService.findAll();
        List<CityAddress> city = cityAddressService.findAll();
        List<AreaAddressVO> area = areaAddressService.findAll();
        List<StreetAddressVO> street = streetAddressService.findAll();
        model.addAttribute("street",street);
        model.addAttribute("area",area);
        model.addAttribute("city",city);
        model.addAttribute("list",list);
        return "jspafter/addrecipient";
    }

    @RequestMapping("addrecipient")
    public String addrecipient(RecipientVO recipientVO){
        logger.info("进入了添加收件人信息控制器");
        Boolean flag = recipientService.insert(recipientVO);
        logger.info(flag?"添加员工成功":"添加员工失败");
        return "redirect:recipient.action";
    }

    @RequestMapping("/delete")
    public String delete(int[] reid){
        logger.info("进入了删除收件人信息控制器");
        if(reid.length>0){
            boolean falg = recipientService.delete(reid);
            logger.info(falg?true:false);
        }
        return "redirect:recipient.action";
    }

    @RequestMapping("/toupdate")
    public String updaterecipient(int reid,Model model){
        logger.info("进入了去修改控制器");
        RecipientVO recipient = recipientService.findByReid(reid);
        List<CityAddress> city = cityAddressService.findAll();
        List<AreaAddressVO> area = areaAddressService.findAll();
        List<StreetAddressVO> street = streetAddressService.findAll();
        model.addAttribute("street",street);
        model.addAttribute("area",area);
        model.addAttribute("city",city);
        model.addAttribute("recipient",recipient);
        logger.info(recipient);
        return "/jspafter/updaterecipient";
    }

    @RequestMapping("/updaterecipient")
    public String update(RecipientVO recipient){
        logger.info("进入了收件人信息修改控制器");
        Boolean flag = recipientService.update(recipient);
        return "redirect:recipient.action";
    }


}
